import { ref, computed,watch } from "vue";
import { useSearchStore } from "../../../../store/search";
import { getFieldsConfig } from "../../../../config";
const searchStore = useSearchStore();
export const useSearchHooks = (emits: any) => {
  const store = useSearchStore();


  // 当前搜索配置
  const currSearchConfig = ref(getFieldsConfig(store)[store.searchType]);

  const searchTypes = ref<any>([
    { key: "1", label: "看资讯" },
    { key: "2", label: "查政策" },
    { key: "3", label: "找部门" },
    { key: "4", label: "查立项" },
    { key: "5", label: "做规划", url: "/matching" },
  ]);
  const currSearchType = ref(store.searchType);
  const superSearchStatus = ref(false)
  const changeSearchType = (type: "1" | "2" | "3" | "4", el: any) => {
    el.tagsEl = [];
    currSearchConfig.value = getFieldsConfig(store)[type];
    currSearchType.value = type;
    store.changeSearchType(type);
    if(searchStore.searchType=='1'){
      superSearchStatus.value = false
    }else{
      superSearchStatus.value = true
    }
  };
  const searchForm = ref<any>({
    keyword: ""
  });
watch(() => currSearchType.value,(newValue, oldValue) => {
  searchForm.value.keyword = ''
})
  /**
   *查询逻辑
   */
  // 高级搜索条件显示状态

  // 子组件发射事件修改queryParams
  const changeQueryParams = (val: any, isReset: any) => {
    searchForm.value = {
      ...val,
      keyword: searchForm.value.keyword,
      searchType: currSearchType.value
    };

    // 组合完参数再进行查询
    if (isReset) {
      emits("resetPageInfo")
    }
    query();

  };

  const query = () => {
    let searchType = "1"
    switch (currSearchType.value) {
      case "1":
        searchType = "1"
        break;
      case "2":
        searchType = "2"
        break;
      case "3":
        searchType = "3"
        break;
      case "4":
        searchType = "4"
        break;
    }
    searchForm.value = {
      ...searchForm.value,
      searchType: searchType
    };
    emits("query", searchForm.value,true,{
      pageNum: 1,
      pageSize: 10
    })
  };

  return {
    currSearchConfig,
    searchForm,
    searchTypes,
    currSearchType,
    changeSearchType,
    superSearchStatus,
    query,
    changeQueryParams,
  };
};
